<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <!-- 在这里使用pubdate为什么是重复的key -->
      <van-cell
        v-for="(item,index) in commentList"

        :key="item.pubdate"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span slot="title" style="color: #466b9d;">{{ item.aut_name }}</span>
        <div slot="label">
          <p style="color: #363636;">{{ item.content }}</p>
          <p>
            <span style="margin-right: 10px;">{{ item.pubdate|fromNow }}</span>
            <van-button size="mini" type="default" @click="clickReplyBtn(index)">回复</van-button>
          </p>
        </div>
        <van-icon slot="right-icon" :name="item.is_liking?'like':'like-o'" @click="clickHeart(item)" />
      </van-cell>
    </van-list>
    <!-- 评论列表 -->

    <!-- 发布评论 -->
    <van-cell-group class="publish-wrap">
      <van-field
        v-model="commentContent"
        clearable
        placeholder="请输入评论内容"
      >
        <van-button slot="button" size="mini" type="info" @click="announceComment">发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /回复弹窗 -->
    <van-popup v-if="isShowCommentReply" v-model="isShowCommentReply" :style="{ height: '85%' }" round position="bottom"><CommentReply :ariticle-id="ariticleId" :comment-content-replay="commentContentReplay" @closeCommentReplyPopup="isShowCommentReply=!isShowCommentReply" /></van-popup>

  </div>
</template>

<script>
import CommentReply from '@/views/article/components/commentReply.vue'
import { reqGetComment, reqAddCommentLike, reqCannelCommentLike, reqAddComment } from '@/api/comment'
export default {
  name: 'ArticleComment',
  components: {
    CommentReply
  },
  props: {
    ariticleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [], // 评论列表
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null,
      commentList: [],
      commentContent: '',
      isShowCommentReply: false,
      commentContentReplay: ''

    }
  },

  methods: {
    async onLoad() {
      // 异步更新数据
      const { data: { last_id, results }} = await reqGetComment('a', this.ariticleId, this.offset)
      this.offset = last_id
      this.commentList.push(...results)
      this.loading = !this.loading
      if (!this.last_id || results.length === 0) {
        this.finished = !this.finished
      }
    },
    async clickHeart(item) {
      item.is_liking ? await reqCannelCommentLike(item.com_id) : await reqAddCommentLike(item.com_id)
      item.is_liking = !item.is_liking
    },
    async announceComment() {
      if (!this.commentContent) return this.$toast.fail('内容不能为空哦')
      const res = await reqAddComment(this.ariticleId, this.commentContent)
      this.commentList.unshift(res.data.new_obj)
      this.commentContent = ''
    },
    clickReplyBtn(index) {
      this.isShowCommentReply = !this.isShowCommentReply
      this.commentContentReplay = this.commentList[index]
    }
  }
}
</script>

<style scoped lang='scss'>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
